import { computed, ref } from "vue"
import { defineStore, acceptHMRUpdate } from "pinia"

export const loginStore = defineStore('login', ()=>{
    const data = ref({
        userId: 0,
        userCode: '',
        userName: '',
        phone: '',
        token: '',
        tokenExpiresAt: '',
        refreshToken:'',
        refreshTokenExpiresAt:'',
        session: ''
    })

    function $reset(){
        data.value = {
            userId: 0,
            userCode:'',
            userName: '',
            phone: '',
            token: '',
            tokenExpiresAt: '',
            refreshToken: '',
            refreshTokenExpiresAt:'',
            session: ''
        }
        localStorage.setItem('self', JSON.stringify(data.value))
    }

    const token = computed(()=>{ 
        getStorage()
        
        return {
            value: data.value.token,
            expiresAt: data.value.tokenExpiresAt
        } 
    })

    const user = computed(()=>{
        getStorage()

        return {
            userId: data.value.userId,
            userCode : data.value.userCode,
            userName : data.value.userName,
            phone: data.value.phone
        }
    })

    const session = computed(() => {
        getStorage()
        return data.value.session
    })

    const refreshToken = computed(()=>{
        getStorage()
        
        return {
            refreshToken: data.value.refreshToken,
            refreshTokenExpiresAt: data.value.refreshTokenExpiresAt
        }
    })

    function setToken(token: string, expiresAt: string, refreshToken:string, refreshTokenExpiresAt: string){
        return new Promise(resolve =>{
            data.value.token = token
            data.value.tokenExpiresAt = expiresAt
            data.value.refreshToken = refreshToken
            data.value.refreshTokenExpiresAt = refreshTokenExpiresAt
            localStorage.setItem('self', JSON.stringify(data.value))
            resolve(0)
        })        
    }

    function setSession(session : string){
        return new Promise(resolve => {
            data.value.session = session
            localStorage.setItem('self', JSON.stringify(data.value))
            resolve(0)
        })
    }

    function setUser(userId: number, userCode: string, userName:string, phone:string){
        return new Promise(resolve =>{
            data.value.userId = userId
            data.value.userCode = userCode
            data.value.userName = userName
            data.value.phone = phone
            localStorage.setItem('self', JSON.stringify(data.value))
            resolve(0)
        })
    }

    function getStorage(){
        const storageData = localStorage.getItem('self')
        if(null != storageData){
            data.value = JSON.parse(storageData) 
        }
    }
    
    return { user, token, session , $reset, refreshToken , setToken, setSession, setUser }
})

if(import.meta.hot){
    import.meta.hot.accept(acceptHMRUpdate(loginStore, import.meta.hot))
}

